package fussbudget.gui;

import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.jdesktop.swingx.JXMultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.VerticalLayout;

import fussbudget.PaymentEntity;
import fussbudget.Transaction;

public class GUI extends JPanel {
	Container				parent;
	JTabbedPane				tabbedPane;

	CalendarMaster			calendarMaster;
	CalendarTablePanel		calendarTablePanel;

	PaymentEntityTablePanel	payeeList;
	PaymentEntityTablePanel	payerList;

	TransactionTablePanel	depositList;
	TransactionTablePanel	paymentList;

	ScheduleTablePanel depositScheduleList;
	ScheduleTablePanel paymentScheduleList;

	public GUI(Container parent) {
		super();
		this.parent = parent;

		tabbedPane = new JTabbedPane();
		tabbedPane.setPreferredSize(new Dimension(parent.getBounds().width, parent.getBounds().height - 50));
		add(tabbedPane);

		setupCalendarPane();
		setupPayerPane();
		setupPayeePane();
		setupTransactionPane();
		setupSchedulePane();

		((Container) parent).add(this);
	}

	private void setupCalendarPane() {
		calendarMaster = new CalendarMaster();
		tabbedPane.addTab("Calendar", calendarMaster.calendarPanel);
		
		calendarTablePanel = new CalendarTablePanel("Calendar", this);
		
		tabbedPane.addTab("Calendar", calendarTablePanel);
	}

	private void setupPayerPane() {
		JPanel panel = new JPanel();
		panel.setLayout(new VerticalLayout());

		payerList = new PaymentEntityTablePanel("Payers", this, PaymentEntity.Type.PAYER);
		panel.add(payerList);

		tabbedPane.addTab("Payers", panel);
	}

	private void setupPayeePane() {
		JPanel panel = new JPanel();
		panel.setLayout(new VerticalLayout());

		payeeList = new PaymentEntityTablePanel("Payees", this, PaymentEntity.Type.PAYEE);
		panel.add(payeeList);

		tabbedPane.addTab("Payees", panel);
	}

	private void setupTransactionPane() {
		JXMultiSplitPane tabPanel = new JXMultiSplitPane();
		String layoutDef = "(COLUMN (LEAF name=top weight=0.5) (LEAF name=bottom weight=0.5))";

		MultiSplitLayout.Node modelRoot = MultiSplitLayout.parseModel(layoutDef);

		tabPanel.getMultiSplitLayout().setModel(modelRoot);

		depositList = new TransactionTablePanel("Deposits", this, Transaction.TYPE.DEPOSIT);
		paymentList = new TransactionTablePanel("Payments", this, Transaction.TYPE.PAYMENT);

		tabPanel.add(depositList, "top");
		tabPanel.add(paymentList, "bottom");

		tabbedPane.addTab("Transactions", tabPanel);
	}
	
	private void setupSchedulePane(){
		JXMultiSplitPane tabPanel = new JXMultiSplitPane();
		String layoutDef = "(COLUMN (LEAF name=top weight=0.5) (LEAF name=bottom weight=0.5))";

		MultiSplitLayout.Node modelRoot = MultiSplitLayout.parseModel(layoutDef);

		tabPanel.getMultiSplitLayout().setModel(modelRoot);

		depositScheduleList = new ScheduleTablePanel("Deposits", this, Transaction.TYPE.DEPOSIT);
		paymentScheduleList = new ScheduleTablePanel("Payments", this, Transaction.TYPE.PAYMENT);

		tabPanel.add(depositScheduleList, "top");
		tabPanel.add(paymentScheduleList, "bottom");

		tabbedPane.addTab("Schedule", tabPanel);
	}
}
